package CutLoad.transitions.cloud
{
	import CutLoad.Base;
	import CutLoad.cookies.Painter;
	
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * 在一个范围里查看比这个范围大的对象时，会根据鼠标的移动，查看这个大对象各个部分。
	 * @author hai
	 * 
	 */	
	public class MouseEasingViewWindow extends Base
	{
		private var target:Base;
		private var viewW:Number,viewH:Number;
		private var offset:Number,mouseXYOffset:Number;
		private var spMask:Sprite;
		private var mouse_x:Number,mouse_y:Number;
		private var mouse_x_last:Number,mouse_y_last:Number;
		private var modeType:Number;
		/**
		 * 初始化 
		 * @param target 要放进去查看的东西
		 * @param w 视窗的宽
		 * @param h 视窗的高
		 * @param modeType 查看模式  1:只是左右可以移动，2：只是上下可以移动,其它值：上下左右都可以移动
		 * @param mouseXYOffset 相对实际鼠标坐标，提前多少。默认为：mouseXYOffset
		 * @param easingOffset  缓动系数 默认为：.2
		 * 
		 */		
		public function MouseEasingViewWindow(target:Base,w:Number,h:Number,modeType:Number=3,mouseXYOffset:Number=20,easingOffset:Number=.2)
		{
			this.modeType=modeType;
			mouse_x=0;
			mouse_y=0;
			mouse_x_last=0;
			mouse_y_last=0
			viewW=w;
			viewH=h;
			this.target=target;
			this.mouseXYOffset=Math.abs(mouseXYOffset);
			this.offset=easingOffset;
			target.show(this);
			spMask=Painter.getRect(w,h,0xff0000,.5);
			this.addChild(spMask);
			target.mask=spMask;
			this.addEventListener(Event.ENTER_FRAME,enter_frame);
		}
		private function enter_frame(e:Event):void{
			if(this.mouseX>0&&this.mouseX<viewW&&this.mouseY>0&&this.mouseY<viewH){
				var cax:Number=mouse_x_last-this.mouseX;
				var cay:Number=mouse_y_last-this.mouseY;
				//只是移动的时候，才做返回，所以mouse_x=this.mouseX;和mouse_y=this.mouseY;要放在检测里面，不然会错的。
				if(cax>0){
					//trace("-------")
					//trace(mouse_x);
					mouse_x=this.mouseX;
					mouse_x-=mouseXYOffset;
					//trace(mouse_x);
					//trace("左")
				}else if(cax<0){
					mouse_x=this.mouseX;
					mouse_x+=mouseXYOffset;
					
					//trace("右")
				}
				if(cay>0){
					mouse_y=this.mouseY;
					mouse_y-=mouseXYOffset;
					//trace("上");
				}else if(cay<0){
					mouse_y=this.mouseY;
					mouse_y+=mouseXYOffset;
					//trace("下")
				}
				//
				mouse_x_last=this.mouseX;
				mouse_y_last=this.mouseY;
			}
			//
			if(mouse_x<0) mouse_x=0;
			if(mouse_y<0) mouse_y=0;
			if(mouse_x>viewW) mouse_x=viewW;
			if(mouse_y>viewH) mouse_y=viewH;
			//
			//trace("::::"+mouse_x+"  "+mouse_y);
			//
			var px:Number=mouse_x/viewW;
			var py:Number=mouse_y/viewH;
			//
			px=-(px*(target.getShowWidth()-viewW));
			py=-(py*(target.getShowHeight()-viewH));
			//
			if(modeType==1){
				target.x+=(px-target.x)*offset;
			}else if(modeType==2){
				target.y+=(py-target.y)*offset;
			}else{
				target.x+=(px-target.x)*offset;
				target.y+=(py-target.y)*offset;
			}
			
		}
		override public function getShowWidth():Number{
			return viewW;
		}
		override public function getShowHeight():Number{
			return viewH;
		}
	}
}